<?php

namespace app\common\model\knowbase;

use app\admin\model\Admin;
use think\Model;


class ProjectPageTemplate extends Model
{

    

    

   
    protected $name = 'knowbase_project_page_template';
    
   
    protected $autoWriteTimestamp = 'integer';

   
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

   
    protected $append = [
        'type_text'
    ];
    
    protected static function init()
    {
        self::afterInsert(function ($row) {
           
           
           
           
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['admin_id'=>\app\admin\library\Auth::instance()->id]);
            if ($row['type']==1) {
               ProjectPageTemplateGroupAccess::updateAccess($row[$pk], $row['auth_group_ids']);
            } else {
                ProjectPageTemplateGroupAccess::updateAccess($row[$pk], []);
            }
            Progress::addLog('template_add',$row[$pk],$row['name']);
        });

        self::afterUpdate(function ($row){
           
           
           
           
            $pk = $row->getPk();
            if ($row['type']==1) {
               ProjectPageTemplateGroupAccess::updateAccess($row[$pk], $row['auth_group_ids']);
            } else {
                ProjectPageTemplateGroupAccess::updateAccess($row[$pk], []);
            }
            if (strpos(request()->url(), '/del')===false && strpos(request()->url(), '/add')===false ){
                Progress::addLog('template_update',$row['id'],$row['name']);
            }
        });
        
        self::afterDelete(function ($row){
           
           
           
           
            $pk = $row->getPk();
            ProjectPageTemplateGroupAccess::updateAccess($row[$pk], []);
            Progress::addLog('template_delete',$row['id'],$row['name']);
        });
    }
    
    
    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function admin()
    {
        return $this->belongsTo(Admin::class, 'admin_id')->setEagerlyType(0);
    }

    public function groups()
    {
        return $this->hasMany(ProjectPageTemplateGroupAccess::class, 'project_page_template_id');
    }

    public function checkAccess()
    {
        if (\app\admin\library\Auth::instance()->isSuperAdmin()) {
            return true;
        }
        
        $data = $this->getData();
        if ($data['type']==0) {
            return true;
        } else if ($data['type']==2 && $data['admin_id']==\app\admin\library\Auth::instance()->id) {
            return true;
        } else if ($data['type']==1) {
            $auth_group_ids  = array_filter(explode(",", $this->getData('auth_group_ids')));
            $groups = array_column(\app\admin\library\Auth::instance()->getGroups(),'group_id');
            if (!$auth_group_ids) {
                return true;
            }
            if (array_intersect($auth_group_ids, $groups)) {
                return true;
            }
        }
        return false;
    }
    
}
